/**
 * 登录模块，提供登录相关业务功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "msgServ", "ajaxServ","formServ","btnServ"],
		function($, msgServ, ajaxServ,formServ,btnServ) {

			'use strict';
			var _buttonConfig;
			
				
			var public_method = {				
				
				init:function(doc){
					private_method.init(doc);
					_buttonConfig={"上传":private_method.uploadFile}
				}
				
			}

			var private_method = {
				init : function(doc) {
					if (window.console) {
					console.info('upload modules gets ready and been loaded.');
					}
					private_method.bindBtnEvent(doc);
				},
				fileSelected:function () {
				  var _files = $('#fileToUpload')
				  var file = _files.files;
				  if (file) {
				    var fileSize = 0;
				    if (file.size > 1024 * 1024)
				      fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB';
				    else
				      fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';
				
				    $('#fileName').textbox('setValue',file.name) ;
				    $('#fileSize').textbox('setValue',fileSize);
				    $('#fileType').textbox('setValue',file.type);
				  }
				},

				uploadFile:function () {
				  var xhr = new XMLHttpRequest();
				  var fd = new FormData($('#upload-form')[0]);
				
				  /* event listners */
				  xhr.upload.addEventListener("progress", private_method.uploadProgress, false);
				  xhr.addEventListener("load", private_method.uploadComplete, false);
				  xhr.addEventListener("error", private_method.uploadFailed, false);
				  xhr.addEventListener("abort", private_method.uploadCanceled, false);
				  /* Be sure to change the url below to the url of your upload server side script */
				  var uploadUrl = "/" + window.location.pathname.split('/')[1] + '/flowDefinition/flowDeploy.action';
				  xhr.open("POST", uploadUrl);
				  xhr.send(fd);
				},

				 uploadProgress:function(evt) {
				  if (evt.lengthComputable) {
				    var percentComplete = Math.round(evt.loaded * 100 / evt.total);
				    $('#progressNumber').innerHTML = percentComplete.toString() + '%';
				  }
				  else {
				    $('#progressNumber').innerHTML = 'unable to compute';
				  }
				},

				 uploadComplete:function(evt) {
				  /* This event is raised when the server send back a response */
				  $('#fileToUpload').files = null;
				  alert(evt.target.responseText);
				},

				 uploadFailed:function(evt) {
				  alert("There was an error attempting to upload the file.");
				},

				 uploadCanceled:function(evt) {
				  alert("The upload has been canceled by the user or the browser dropped the connection.");
				},
				
				
				//绑定按钮事件
				bindBtnEvent:function(doc){
					// 绑定按钮事件
					var btn = $(doc).find('div.upload-flowfile a.function-btn')
					$(btn).unbind('click');
					$(btn).bind('click', private_method.function_btn_listenner)	
					
					
				},
				
				
				//功能按钮监听函数
				function_btn_listenner:function(e){
					btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})						
				}				
				

			};
			
			return public_method;

		});